//
//  imprint
//
//  Created by 小柠檬🍋 on 2025/1/16.
//

import Foundation
import LeanCloud

//我的音色
class MyVoiceViewModel : BaseViewModel {
    @Published var items = [AiVoiceBean]()
    @Published var status = ""
    
    override init() {
        super.init()
        self.addObserve(self,  .buyVoice) { (seleInstance, isBought: Bool) in
            showLog("刷新我的音色")
            seleInstance.getData(true)
        }
    }
    
    func getData(_ isRefresh: Bool = false) {
        if isRefresh {
            status = ""
            items.removeAll()
        }
        requestList(isRefresh: isRefresh) {
            try check(userManager.isLogin(), "登陆后才能查看我的音色")
            let query = LCQuery(className: "AiVoice")
            query.whereKey("userId", .equalTo(userManager.getUserId()))
            query.whereKey("status", .ascending) //升序
            showLog("加载页数：\(self.pageNo)")
            query.limit = self.pageSize
            query.skip = self.pageNo * self.pageSize
            let list = try await LeancloudManager.instance.getList(query: query, modeType: AiVoiceBean.self)
            try check(!isRefresh || !list.isEmpty, "暂无音色")
            return list
        } suc: {
            self.status = ""
            self.items.append(contentsOf: $0)
        } fail: {
            self.status = $0.message
        }
    }
    
}
